package com.hehao.java_basic.doc_poi.demoEntity;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author Damon
 * @date 2023/9/15
 * @time 11:28
 */
@Slf4j
public class DemoImportListener extends AnalysisEventListener<EasyExcelDemoData> {

    List<String> codes = Lists.newArrayList();
    List<String> errors = Lists.newArrayList();

    /**
     * 每解析一行，回调该方法
     * @param data
     * @param context
     */
//    @Override
//    public void invoke(Object data, AnalysisContext context) {
//        String code = ((EasyExcelDemoData) data).getCode();
//        Integer num = ((EasyExcelDemoData) data).getNum();
//        if (StringUtils.isEmpty(code)) {
////            throw new RuntimeException(String.format("第%s行卡密为空，请核实", context.readRowHolder().getRowIndex() + 1));
//            throw new RuntimeException(String.format("序号%s卡密为空，请核实",num));
//        }
//        if (codes.contains(code)) {
////            throw new RuntimeException(String.format("第%s行卡密已重复，请核实", context.readRowHolder().getRowIndex() + 1));
//            throw new RuntimeException(String.format("序号%s卡密已重复，请核实", num));
//        } else {
//            codes.add(code);
//        }
//    }

    /**
     * When analysis one row trigger invoke function.
     *
     * @param data    one row value. It is same as {@link AnalysisContext#readRowHolder()}
     * @param context analysis context
     */
    @Override
    public void invoke(EasyExcelDemoData data, AnalysisContext context) {
        String code = data.getCode();
        Integer num = data.getNum();
        if (StringUtils.isEmpty(code)) {
            errors.add(String.format("序号%s卡密为空，请核实", num));
        }
        if (codes.contains(code)) {
            errors.add(String.format("序号%s卡密已重复，请核实", num));
        } else {
            codes.add(code);
        }
        List<String> options = Lists.newArrayList();
        ReadRowHolder readRowHolder = context.readRowHolder();
        Map<Integer, Cell> cellMap = readRowHolder.getCellMap();
        for (int i = 2; i < cellMap.size(); i++) {
            Cell cell = cellMap.get(i);
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(cell));
            String stringValue = jsonObject.get("stringValue").toString();
            options.add(stringValue);
        }
        data.setOptions(options);
        log.info("====>"+JSON.toJSONString(options));
    }

    /**
     * 解析完全部回调
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

        try {
            throw new RuntimeException(JSON.toJSONString(errors));
        } finally {
            errors.clear();
            log.info("===执行清理读取信息===");
        }
    }
}
